# Copyright (C) 2007-2011 Michael Foord & the mock team
# E-mail: fuzzyman AT voidspace DOT org DOT uk
# http://www.voidspace.org.uk/python/mock/

import sys

from tests.support import unittest2

from mock import sentinel, Mock
import mock


# for use in the test
something = sentinel.Something
something_else = sentinel.SomethingElse

PTModule = sys.modules[__name__]


class PatcherTest(unittest2.TestCase):

    def test_add_should_patch_dotted_name_and_return_mock(self):
        patches = mock.patcher()
        patched = patches.add('%s.something' % __name__, sentinel.Something2)
        self.assertEqual(sentinel.Something2, patched)
        patches.add('%s.something_else' % __name__, sentinel.Something3)

        self.assertEqual(PTModule.something, sentinel.Something2,
                         "reseted")
        self.assertEqual(PTModule.something_else, sentinel.Something3,
                         "reseted")
        patches.reset()
        self.assertEqual(PTModule.something, sentinel.Something,
                         "patch not restored")
        self.assertEqual(PTModule.something_else, sentinel.SomethingElse,
                         "patch not restored")

    def test_add_object_should_delegate_to_patch_object(self):
        patches = mock.patcher()
        subject = Mock()
        subject.foo = sentinel.Something
        patches.add_object(subject, 'foo', sentinel.Something2)
        self.assertEqual(subject.foo, sentinel.Something2, "unpatched")
        patches.reset()
        self.assertEqual(subject.foo, sentinel.Something, "patch not restored")

    def test_add_dict_should_delegate_to_patch_dict(self):
        patches = mock.patcher()
        subject = {}
        subject['key'] = sentinel.Something
        patches.add_dict(subject, {'key': sentinel.Something2})
        self.assertEqual(subject['key'], sentinel.Something2, "unpatched")
        patches.reset()
        self.assertEqual(subject['key'], sentinel.Something,
                         "patch not restored")

    def test_reset_multiple_times_does_no_harm(self):
        patches = mock.patcher()
        subject = Mock()
        subject.foo = sentinel.Something
        patches.add_object(subject, 'foo', sentinel.Something2)
        self.assertEqual(subject.foo, sentinel.Something2, "unpatched")
        patches.reset()
        self.assertEqual(subject.foo, sentinel.Something, "patch not restored")
        patches.reset()
        self.assertEqual(subject.foo, sentinel.Something, "patch not restored")


if __name__ == '__main__':
    unittest2.main()
